using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Openfree.Framework.DataAccess;

public partial class PortalPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ReInitHeader();
        if (!string.IsNullOrEmpty(SessionManager.Username))
        {
            hlUsername.Text = SessionManager.Username;
            pnlLoginInfo.Visible = true;
            pnlLogin.Visible = false;
        }
        else
        {
            pnlLoginInfo.Visible = false;
            pnlLogin.Visible = true;
        }
    }

    protected virtual void ReInitHeader()
    {
        int count = Page.Header.Controls.Count - 1;
        for (int i = count; i >= 0; i--)
        {
            HtmlControl c = Page.Header.Controls[i] as HtmlControl;
            HtmlTitle t = Page.Header.Controls[i] as HtmlTitle;
            if (c != null && c.Attributes["type"] != null &&
                c.Attributes["type"].Equals("text/css", StringComparison.OrdinalIgnoreCase))
            {
                Page.Header.Controls.Remove(c);
            }

            if (t != null)
            {
                Page.Header.Controls.Remove(t);
            }
        }

        string title = "\r\n    <title>{0}</title>\r\n";
        string script = "    <script src=\"App_Themes/"+ Page.Theme + 
            "/{0}\" type=\"text/javascript\"></script>\r\n";
        string link = "    <link href=\"App_Themes/" + Page.Theme + 
            "/{0}\" type=\"text/css\" rel=\"stylesheet\" />\r\n";
        count = SessionManager.RootRUL.LastIndexOf('/');
        if (Request.Url.ToString().LastIndexOf('/') > count)
        {
            script = "   <script src=\"../App_Themes/" + Page.Theme +
            "/{0}\" type=\"text/javascript\"></script>\r\n";
            link = "    <link href=\"../App_Themes/" + Page.Theme +
            "/{0}\" type=\"text/css\" rel=\"stylesheet\" />\r\n";
        }


        if (Page.Title == null || Page.Title == "Untitled Page")
        {
            Page.Title = ConfigManager.SiteSubject;
        }
        Literal litTitle = new Literal();
        litTitle.Text = string.Format(title, Page.Title);
        Literal litScript = new Literal();
        litScript.Text = string.Format(script, "DropDownList.js") +
            string.Format(script, "showTip.js") +
            string.Format(script, "share.js");            

        Literal litLink = new Literal();
        litLink.Text = string.Format(link, "00.share.css") +
            string.Format(link, "21.hotImage.css") +
            string.Format(link, "01.gridView.css") +
            string.Format(link, "02.portalFrame.css");

        Page.Header.Controls.Add(litTitle);
        Page.Header.Controls.Add(litScript);        
        Page.Header.Controls.Add(litLink);
    }

    protected void btnLogout_Click(object sender, EventArgs e)
    {
        SessionManager.UserID = string.Empty;
        SessionManager.Username = string.Empty;
        SessionManager.LoginName = string.Empty;
        SessionManager.Password = string.Empty;
        SessionManager.UserRole = string.Empty;
        Response.Redirect("~/Index.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}
